// const { axios } = require("./index");
import axios from "./index";
import { Rank, Division } from "../constant/rank";
// impoprt  { } from '@/'
import { Compare } from "@/utils/index";
// 查找召唤师
export const findSummoner = async (name: string) =>
  await axios.get("/lol-summoner/v1/summoners", {
    params: {
      name,
    },
  });

// 接受对局
export const confirmTheGame = async () =>
  await axios.post("/lol-matchmaking/v1/ready-check/accept");

type userT = {
  name: string;
  level: number;
  avatar: number;
  xpSinceLastLevel: number;
  xpUntilNextLevel: number;
  infoId: number;
};
// 获取召唤师信息
export const getSummoner = async () => {
  // 返回用户数据
  let user: userT = {
    name: "",
    level: 0,
    avatar: 0,
    xpSinceLastLevel: 0,
    xpUntilNextLevel: 0,
    infoId: 0,
  };

  // 获取召唤师信息
  let data = (await axios.get("/lol-summoner/v1/current-summoner")) as any;
  // if(data)
  if (data.message === "请求超时" || !data) {
    return undefined;
  }
  // 名字
  user.name = data.displayName;
  // 等级
  user.level = data.summonerLevel;
  // 头像id
  user.avatar = data.profileIconId;
  // 当前经验
  user.xpSinceLastLevel = data.xpSinceLastLevel;
  // 升级所需经验
  user.xpUntilNextLevel = data.xpUntilNextLevel;
  //召唤师信息id
  user.infoId = data.summonerId;

  return user;
};

// 获取点券
export const getRP = async () => {
  // 点券
  let { RP } = (await axios.get("/lol-inventory/v1/wallet/RP")) as {
    RP: number;
  };
  return RP;
};

// 获取精粹
export const getLolBlueEssence = async () => {
  // 蓝色精粹
  let { lol_blue_essence } = (await axios.get(
    "/lol-inventory/v1/wallet/lol_blue_essence"
  )) as { lol_blue_essence: number };

  return lol_blue_essence;
};

// 获取召唤师大区
export const getEnvironment = async () => {
  // 召唤师大区
  let { environment } = (await axios.get(
    "/riotclient/v1/crash-reporting/environment"
  )) as { environment: string };
  environment = environment.toUpperCase();

  const Telecommunication = [
    "艾欧尼亚",
    "祖安",
    "诺克萨斯",
    "班德尔城",
    "皮尔特沃夫",
    "战争学院",
    "巨神峰",
    "雷瑟守备",
    "裁决之地",
    "黑色玫瑰",
    "暗影岛",
    "钢铁烈阳",
    "水晶之痕",
    "均衡教派",
    "影流",
    "守望之海",
    "卡拉曼达",
    "皮城警备",
  ];
  const Unicom = [
    "比尔吉沃特",
    "德玛西亚",
    "弗雷尔卓德",
    "无畏先锋",
    "怒瑞玛",
    "扭曲丛林",
    "巨龙之巢",
  ];
  const netcom = ["男爵领域", "峡谷之巅"];

  if (environment.indexOf("HN") > -1) {
    // 电信
    let i = environment.slice(2, 3) as unknown as number;

    environment = Telecommunication[i - 1];
  } else if (environment.indexOf("WT") > -1) {
    // 联通

    let i = environment.slice(2, 3) as unknown as number;

    environment = Unicom[i - 1];
  } else if (environment.indexOf("EDU") > -1) {
    // 教育网
    environment = "教育网";
  } else if (environment.indexOf("BGP") > -1) {
    // 双线
    let i = environment.slice(3, 4) as unknown as number;

    environment = netcom[i - 1];
  }

  return environment;
};

// 获取召唤师荣誉
export const getHonor = async () => await axios.get("/lol-honor-v2/v1/profile");

// 获取召唤师 排位信息
export const getRank = async () => {
  // 排位信息
  // const { Rank, Division } = require("../constant/rank");

  let { queueMap } = (await axios.get(
    "/lol-ranked/v1/current-ranked-stats"
  )) as any;
  let rank: any = {};
  // 单双排
  let rankSolo: any = {};
  // 灵活组排
  let rankSr: any = {};
  // 云顶之弈
  let rankTft: any = {};
  for (const key in queueMap) {
    if (Object.hasOwnProperty.call(queueMap, key)) {
      const element = queueMap[key];

      switch (key) {
        // 单双排
        case "RANKED_SOLO_5x5":
          rankSolo.tier =
            element.tier !== "NONE" ? (Rank as any)[element.tier] : "未定级";
          rankSolo.division = (Division as any)[element.division];
          rankSolo.wins = element.wins;
          rankSolo.losses = element.losses;
          rankSolo.leaguePoints = element.leaguePoints;
          break;

        // 灵活组排
        case "RANKED_FLEX_SR":
          rankSr.tier =
            element.tier !== "NONE" ? (Rank as any)[element.tier] : "未定级";
          rankSr.division = (Division as any)[element.division];
          rankSr.wins = element.wins;
          rankSr.losses = element.losses;
          rankSr.leaguePoints = element.leaguePoints;
          break;

        // 云顶之弈
        case "RANKED_TFT":
          rankTft.tier =
            element.tier !== "NONE" ? (Rank as any)[element.tier] : "未定级";
          rankTft.division = (Division as any)[element.division];
          rankTft.wins = element.wins;
          rankTft.losses = element.losses;
          rankTft.leaguePoints = element.leaguePoints;
          break;

        default:
          break;
      }
    }
  }

  // 单双排
  rank.rankSolo = rankSolo;
  // 灵活组排
  rank.rankSr = rankSr;
  // 云顶之弈
  rank.rankTft = rankTft;

  return rank;
};

// 获取头像
export const getAvatarUrl = async () => {
  let { profileIconId } = (await axios.get(
    "/lol-summoner/v1/current-summoner"
  )) as any;

  // let data = await axios({
  //   method: "get",
  //   url: `/lol-game-data/assets/v1/profile-icons/${profileIconId}.jpg`,
  //   responseType: "arraybuffer", //这里特殊注明返回格式是文件流
  // });

  // let data = await axios(`/lol-game-data/assets/v1/profile-icons/${profileIconId}.jpg`,)
  let data = await axios.get(
    `/lol-game-data/assets/v1/profile-icons/${profileIconId}.jpg`,
    { responseType: "arraybuffer" }
  );

  return data;
};

//  自动寻找对局
export const lookingForGame = async () =>
  await axios.post("/lol-lobby/v2/lobby/matchmaking/search");

// 玩家点赞
// 随机数
function randomNum(minNum: any, maxNum: any) {
  return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
}

export const giveALike = async () => {
  let data = (await axios.get("/lol-end-of-game/v1/eog-stats-block")) as any;
  (window as any).config.setLog("点赞中");
  if (!data.gameId) {
    return;
  }
  (window as any).config.setLog({ data: data });

  // 对局房间id
  let { gameId } = data;
  // 召唤师自己id
  let { summonerId } = data.localPlayer;

  // 队友id
  let summonerIdS = [];
  for (let i = 0; i < data.teams[0].players.length; i++) {
    const elId = data.teams[0].players[i].summonerId;
    if (elId !== summonerId) {
      summonerIdS.push(elId);
    }
  }

  let randomOne = randomNum(0, 3);
  randomOne = randomNum(0, 2);

  // 点赞类型
  //  SHOTCALLER COOL HEART OPT_OUT

  const giveThumbsUp = ["SHOTCALLER", "COOL", "HEART"];

  let result = await axios.post("/lol-honor-v2/v1/honor-player", {
    gameId,
    summonerId: summonerIdS[randomOne],
    honorCategory: giveThumbsUp[randomOne],
  });
  return result;
};

// 再来一局
export const oneMoreGame = async () =>
  await axios.post("/lol-lobby/v2/play-again");

// 创建对局房间
export const roomCreation = async (id: number) =>
  await axios.post("/lol-lobby/v2/lobby", {
    queueId: id,
  });

// 创建5v5训练营
export const createTrainingRoom = async () =>
  await axios.post("/lol-lobby/v2/lobby", {
    customGameLobby: {
      configuration: {
        gameMode: "PRACTICETOOL",
        gameMutator: "",
        gameServerRegion: "",
        mapId: 11,
        mutators: {
          id: 1,
        },
        spectatorPolicy: "AllAllowed",
        teamSize: 5,
      },
      lobbyName: "5v5 训练营",
      lobbyPassword: null,
    },
    isCustom: true,
  });
